package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @Author mr.wei
 * @Date 2025/1/5 上午11:12
 */
@Component
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg){
        System.out.println("接收到simple.queue的消息：" + msg);
    }

    @RabbitListener(queues = "work.queue")
    public void work(String msg) throws InterruptedException {
        System.out.println("work 接收到work.queue的消息：" + msg);
        //睡眠
        TimeUnit.MILLISECONDS.sleep (10);
    }
    @RabbitListener(queues = "work.queue")
    public void workWork(String msg) throws InterruptedException {
        System.out.println("workwork 接收到work.queue的消息：" + msg);
        TimeUnit.MILLISECONDS.sleep (100);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void fanout1(String msg){
        System.out.println("fanout1 接收到fanout.queue1的消息：" + msg);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void fanout2(String msg){
        System.out.println("fanout2 接收到fanout.queue2的消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding (value = @Queue(name = "direct.queue1"),exchange = @Exchange(name = "itcast.direct"),key = {"red","blue"}))
    public void direct1(String msg){
        System.out.println("direct1 接收到direct.queue1的消息：" + msg);
    }
    @RabbitListener(bindings = @QueueBinding (
            value = @Queue(name = "direct.queue2"),
            exchange =@Exchange(name = "itcast.direct") ,
            key ={"yellow","red"}
    ))
    public void direct2(String msg){
        System.out.println("direct2 接收到direct.queue2的消息：" + msg);
    }
//    @RabbitListener(bindings = @QueueBinding (
//            value = @Queue(name = "1"),
//            exchange = @Exchange(name = "itcast.1",type = ExchangeTypes.DIRECT),
//            key = {"blue"}
//    ))

    @RabbitListener(bindings = @QueueBinding (
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key ={"a.#"}
    ))
    public void topic1(String msg){
        System.out.println("topic1 接收到topic.queue1的消息：" + msg);
    }

    @RabbitListener(bindings =@QueueBinding (
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key ={"*.b"}
    ))
    public void topic2(String msg){
        System.out.println("topic2 接收到topic.queue2的消息：" + msg);
    }

    @RabbitListener(queues = "object.queue")
    public void object(Map<String, Object> msg){
        System.out.println("object 接收到object.queue的消息：" + msg);
    }



}
